package com.example.administrator.school;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.MenuItem;
import android.widget.FrameLayout;

import com.example.administrator.school.fragment.FileFragment;
import com.example.administrator.school.fragment.HomeFragment;
import com.xiumiing.uilibrary.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity {

    @BindView(R.id.FrameLayout)
    FrameLayout mFrameLayout;
    @BindView(R.id.BottomNavigationView)
    BottomNavigationView mBottomNavigationView;
    private HomeFragment mHomeFragment;
    private FileFragment mFileFragment;
    private List<Fragment> mFragments = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mImmersionBar.statusBarColorInt(Color.TRANSPARENT).init();
//        mImmersionBar.hideBar(FLAG_HIDE_BAR);
        ButterKnife.bind(this);
        mHomeFragment = HomeFragment.newInstance("主页");
        mFileFragment = FileFragment.newInstance("文件");
        mHomeFragment = HomeFragment.newInstance("主页");
        mHomeFragment = HomeFragment.newInstance("主页");
        mFragments.add(mHomeFragment);
        mFragments.add(mFileFragment);
        mFragments.add(mHomeFragment);
        mFragments.add(mHomeFragment);
        show(mHomeFragment);
        //为底部导航设置条目选中监听
        mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.item1:
                        show(mHomeFragment);
                        break;
                    case R.id.item2:
                        show(mFileFragment);
                        break;
                    case R.id.item3:
                        startActivity(new Intent(MainActivity.this, UpDataActivity.class));
                        break;
                    case R.id.item4:
                        break;
                    case R.id.item5:
                        break;
                }

                return true;    //这里返回true，表示事件已经被处理。如果返回false，为了达到条目选中效果，还需要下面的代码
                // item.setChecked(true);  不论点击了哪一个，都手动设置为选中状态true（该控件并没有默认实现)
                // 。如果不设置，只有第一个menu展示的时候是选中状态，其他的即便被点击选中了，图标和文字也不会做任何更改

            }
        });

        //默认选中底部导航栏中的第三个
        mBottomNavigationView.getMenu().getItem(2).setChecked(true);


    }

    private void show(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        for (Fragment fragmen : mFragments) {
            if (fragmen.isAdded()) {
                ft.hide(fragmen);
            }
        }
        if (!fragment.isAdded()) {
            ft.add(R.id.FrameLayout, fragment);
        }
        ft.show(fragment);
        ft.commit();
    }

}
